<template>
  <el-upload
    :action="actionApi"
    :show-file-list="false"
    name="file"
    :auto-upload="true"
    :headers="{ authorization: token, 'Request-from': RequestFrom }"
    :before-upload="beforeUpload"
    :on-success="handleSuccess"
    ref="upload"
    :data="{ category_id: categoryId }"
  >
    <template #trigger>
      <el-button type="primary">上传</el-button>
    </template>
  </el-upload>
</template>
<script setup lang="ts">
import { uploadFile } from '@/composables/useUpload'
import { Code } from '@/enum/app'
import Message from '@/support/message'

defineProps({
  categoryId: {
    type: Number,
    default: 0
  }
})

const emits = defineEmits(['refresh'])
const { upload, actionApi, beforeUpload, token, RequestFrom } = uploadFile('upload/file', false)
const handleSuccess = (response: any, uploadFile: any) => {
  if (response.code === Code.SUCCESS) {
    emits('refresh')
  } else {
    Message.error(response.message)
  }
}
</script>
